package com.PVZ.controller;

import com.PVZ.engine.Cleaner;
import com.PVZ.engine.Judgement;
import com.PVZ.engine.Stage;
import com.PVZ.engine.map.GameMap;
import com.PVZ.engine.map.MapBlock;
import com.PVZ.model.cards.PlantCard;
import com.PVZ.model.emuns.CardType;
import com.PVZ.model.emuns.Configs;
import com.PVZ.model.emuns.PlantType;
import com.PVZ.model.plant.Plant;
import com.PVZ.utils.Util;
import com.PVZ.view.CardView;
import com.PVZ.view.GameFrame;
import com.PVZ.view.canvas.EntityCanvas;
import com.PVZ.view.canvas.ToolsCanvas;
import lombok.Data;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

/**
 * Created by @Author: kabuda on @Date: 2021/5/19 14:35
 * <p>
 * 处理点击事件
 */
@Data
public class MouseController {

    private GameFrame gameFrame;
    private Point point;
    private Stage stage;
    private MapBlock[][] mapBlocks;
    private int number;

    /**
     * 点击卡片后显示的图片
     */
    private JLabel pic;

    private CardView cardView;
    private Cleaner cleaner;

    long i = 0;

    /**
     * 是否已经点过
     * true表示已经点过该卡槽 false表示没点过
     */
    private Boolean clickFlag = false;

    private CardType cardType;

    private PlantType plantType;

    private ImageIcon imageIcon;

    private Judgement judgement;

    public MouseController(GameFrame gameFrame, Stage stage, CardView cardView, Cleaner cleaner, Judgement judgement) {
        this.gameFrame = gameFrame;
        this.cardView = cardView;
        this.cleaner = cleaner;
        this.judgement = judgement;

        this.stage = stage;
        this.mapBlocks = GameMap.blocks;

        imageIcon = new ImageIcon();
        pic = new JLabel(imageIcon);
        pic.setLocation(-100, -100);

        JPanel entityCanvas = gameFrame.getEntityCanvas();
        JPanel toolsCanvas = gameFrame.getToolsCanvas();
        toolsCanvas.add(pic);
        toolsCanvas.repaint();

        mouseListener(toolsCanvas);
        mouseMotionListener(toolsCanvas);

    }

    /**
     * 处理鼠标点击 单元格内可拾取物品
     */
    public void mouseListener(JComponent jPanel) {
        jPanel.addMouseListener(new MouseListener() {
            @Override
            public void mouseClicked(MouseEvent e) {

                System.out.println("你点击了" + i++);
                if (clickFlag) {
                    if (cardType == CardType.Plant) {
                        //点击的是植物卡
                        MapBlock block = Util.getBlockByPoint(e.getPoint());
                        //在该单元格里
                        //生成植物
                        if (block != null && !block.isHasPlant()) {
                            PlantCard plantCard = CardView.upCards.get(number);
                            cardView.plantCard(plantCard, block);
                            Stage.sunQuantity-=plantCard.getValue();
                        }

                    } else if (cardType == CardType.Shove) {
                        //点击的是铲子
                        MapBlock block = Util.getBlockByPoint(e.getPoint());

                        if (block != null) {
                            if (block.isHasPlant()) {
                                //将该植物Health设置为-1 待Cleaner清除
                                cleaner.clearSinglePlant(block);
                            }
                        }
                    }
                    clickFlag = false;
                    pic.setIcon(null);
                } else {
                    //第一次点击
                    int i = 0;
                    for (PlantCard upCard : CardView.upCards) {
                        if (e.getX() > CardView.START_X + i * PlantCard.WIDTH && e.getX() < CardView.START_X + PlantCard.WIDTH + i * PlantCard.WIDTH
                                && e.getY() > 10 && e.getY() < 10 + PlantCard.HEIGHT) {
                            //判断点到了哪个card
                            if (upCard.getClickStatus()&&Stage.sunQuantity>=upCard.getValue()) {
                                cardType = CardType.Plant;
                                plantType = upCard.getPlantType();

                                Plant newPlant = Configs.plantMap.get(plantType);
                                //产生植物卡片图片
                                imageIcon = new ImageIcon(newPlant.getImgSrc());
                                pic.setIcon(imageIcon);

                                clickFlag = true;

                                //设置卡片进入冷却
                                number = i;
                                System.out.println("你点击了" + cardType + ":" + plantType);

                                break;
                            }
                        }
                        i++;
                    }
                    if (e.getX() > 130 + 440 && e.getX() < 130 + 440 + 90 && e.getY() > 0 && e.getY() < 80) {   //其他格子每次将X加50即可
                        //点的是铲子
                        clickFlag = true;
                        cardType = CardType.Shove;
                        pic.setIcon(new ImageIcon("res/img/card/shovel.png"));

                        System.out.println("你点击了:" + cardType);
                    }
                    //后面还有菜单等等.....
                }
            }

            @Override
            public void mousePressed(MouseEvent e) {
                //判断单元格中是否有可拾取的对象
                if (!clickFlag) {
                    MapBlock block = judgement.clickJudge(e.getPoint());
                    if (block != null) {
                        //开始进行判断

                        //处理点击事件
                        judgement.clickGetJudge(e.getPoint(), block);
                        System.out.println("点击了" + block);
                    }
                }
            }

            @Override
            public void mouseReleased(MouseEvent e) {
            }

            @Override
            public void mouseEntered(MouseEvent e) {
            }

            @Override
            public void mouseExited(MouseEvent e) {
            }
        });
    }

    public void mouseMotionListener(JComponent jComponent) {
        jComponent.addMouseMotionListener(new MouseMotionListener() {
            @Override
            public void mouseDragged(MouseEvent e) {

            }

            @Override
            public void mouseMoved(MouseEvent e) {
                if (clickFlag) {
                    //已经点过了
                    if (!(e.getY() <= 0 & e.getX() <= 0)) {
                        pic.setLocation(e.getX() - 30, e.getY() - 40);
                    }
                }
            }
        });
    }
}
